package com.itheima.jobs;

import com.itheima.constant.RedisConstant;
import com.itheima.utils.QiniuUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;
import redis.clients.jedis.JedisPool;

import java.util.Set;


@Component("clearJobs")
public class ClearJobs {

    @Autowired
    private JedisPool jedisPool;

    public void clearImg() {
        System.out.println("开始执行清理任务");
        //根据Redis中保存的2个集合进行差值计算,计算出垃圾图片
        Set<String> set = jedisPool.getResource().sdiff(RedisConstant.SETMEAL_PIC_RESOURCES, RedisConstant.SETMEAL_PIC_DB_RESOURCES);
        if (set != null) {
            for (String picName : set) {
                //删除七牛云服务器上的垃圾图片
                QiniuUtils.deleteFileFromQiniu(picName);

                //从Redis中删除垃圾图片的名称
                jedisPool.getResource().srem(RedisConstant.SETMEAL_PIC_RESOURCES,picName);
                System.out.println(picName + "图片已被删除");
            }

        }
    }
}
